import java.sql.*;

/**
 * @Question Dao
 * @Date 2022/12/3 20:01
 * @Solution
 */
public class Dao {
    public void insert() throws SQLException {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBUtil.getConnection();
            String sql="insert into student values(1,'张三',18,2)";
            statement=connection.prepareStatement(sql);
            int ret = statement.executeUpdate();
            if(ret==1) {
                System.out.println("插入成功");
            } else {
                System.out.println("插入失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            connection.close();
            statement.close();
        }
    }
    public void delete() throws SQLException {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBUtil.getConnection();
            String sql="delete from student where name<=>'张三'";
            statement=connection.prepareStatement(sql);
            int ret = statement.executeUpdate();
            if(ret==1) {
                System.out.println("删除成功");
            } else {
                System.out.println("删除失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            connection.close();
            statement.close();
        }
    }
    public void update() throws SQLException {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBUtil.getConnection();
            String sql="update student set id=2 where name<=>'张三'";
            statement=connection.prepareStatement(sql);
            int ret = statement.executeUpdate();
            if(ret==1) {
                System.out.println("修改成功");
            } else {
                System.out.println("修改失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            connection.close();
            statement.close();
        }
    }
    public void search() throws SQLException {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            String sql="select * from student";
            statement=connection.prepareStatement(sql);
            resultSet = statement.executeQuery(sql);
            while(resultSet.next()) {
                System.out.println(resultSet.getInt("id"));
                System.out.println(resultSet.getString("name"));
                System.out.println(resultSet.getInt("age"));
                System.out.println(resultSet.getInt("grade"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            connection.close();
            statement.close();
        }
    }
}
